#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2020/2/21 13:22
# @Author  : HaoXuan
# @Email   : 879316712@qq.com
# @File    : Tencent.py
# @Software: PyCharm

import requests
import time

url = "https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1582264146900&countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=python&pageIndex=1&pageSize=10&language=zh-cn&area=cn"
for page in range(1,10):
    # response = requests.get(url)
    # print(response)
    #请求参数
    params = {
        'timestamp': str(time.time()),  # '1582264946809',     #时间戳
        'keyword': 'python',  # 岗位名称
        'pageIndex': page,            #1,  # 页数
        'pageSize': 10,  # 每个页面展示的数据有多少条
        'language': 'zh-cn',  # 中国简体汉字
        'area': 'cn'
    }
    response = requests.get(url, params=params).json()
    data = response["Data"]["Posts"]  # 取到id后的数据
    # 三种遍历情况
    # 001posts = map(data)   map是一种遍历效果
    # 002[i for i in data]

    #数据持久化（写数据库，写文件）
    #csv文件，文本格式，可用excle打开
    f = open("TencentPin.csv",'w',encoding='utf-8')
    #写表头
    f.write('RecruitPostName,LocationName')
    for i in data:
        temp={
            # print(i)
            # 岗位名称
            'RecruitPostName' : i['RecruitPostName'],  # 键值对
            # 岗位城市
            'LocationName ':i['LocationName']
        }
        f.write('{},{}'.format(**temp))
f.close()
